import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import {
  getDepartmentList,
  getDoctorsList,
  getExaminationList,
  getIllnessList,
  getNurseLists,
  getPatientDetailInfo,
  getPatientRate,
  getPatientsUserInfo,
  getTaskExaminationByUserId,
  getTaskList,
  modifyPatientInfor,
  updateExamination,
  updateTask,
  updateTaskExamination,
} from "../../service/modules/patient";

interface PatientState {
  patientUsersInfo: any[];
  doctorList: any[];
  nurseList: any[];
  deptList: any[];
  illnessList: any[];
  total: number;
  examinationList: any[];
  modificationAndTaskList: any;
  taskList: any[];
  patientDetailInfo: any;
  getRate: any;
}
const initialState: PatientState = {
  patientUsersInfo: [],
  doctorList: [],
  nurseList: [],
  deptList: [],
  illnessList: [],
  total: 10,
  examinationList: [], //检查任务
  modificationAndTaskList: {},
  taskList: [],
  patientDetailInfo: {},
  getRate: {},
};

/**
 * @分页查询用户信息
 */

export const fetchPatientsUserInfoAction = createAsyncThunk(
  "patient/userinfor",
  async (body: { pageNum: number; pageSize: number }) => {
    const res = await getPatientsUserInfo(body);
    // console.log(res);
    return res;
  }
);
/**
 * @医生加护士
 */
export const fetchDoctorListAction = createAsyncThunk(
  "patient/doctorList",
  async (body: { deptId: ""; doctorId: ""; nurseId: "" }) => {
    const res = await getDoctorsList(body);
    return res.data;
  }
);
export const fetchNurseListAction = createAsyncThunk(
  "patient/nurseList",
  async (body: { deptId: ""; doctorId: ""; nurseId: "" }) => {
    const res = await getNurseLists(body);
    return res.data;
  }
);
/**
 * @部门列表
 */
export const fetchDeptListAction = createAsyncThunk(
  "patient/deptList",
  async () => {
    const res = await getDepartmentList();
    return res;
  }
);
/**
 * @病例列表
 */
export const fetchIllnessListAction = createAsyncThunk(
  "patient/illnessList",
  async () => {
    const res = await getIllnessList();
    return res.data;
  }
);

/**
 * @检查列表
 */
export const fetchExaminationListAction = createAsyncThunk(
  "patient/checkModificationList",
  async () => {
    const res = await getExaminationList();
    // console.log(res);
    return res.data;
  }
);
/**
 * @病人对应检查和任务
 */

export const fetchGetTaskExaminationAction = createAsyncThunk(
  "patient/getExaminationByUserId",
  async (query: { userId: number }) => {
    const res = await getTaskExaminationByUserId(query);
    // console.log(res);
    return res.data;
  }
);
/**
 * @更新检查
 */
export const fetchUpdateExaminationAction = createAsyncThunk(
  "patient/updateExamination",
  async (body: {
    userId: number;
    examinationList: Array<{
      userRelevancyId?: number;
      id: number | null;
      timeOn: string | null;
      state: string | null;
    }>;
    examinationListUpdate: any[];
  }) => {
    const res = await updateExamination(body);
    // console.log(res);
    return res;
  }
);
/**
 * @任务列表
 */
export const fetchGetTaskListAction = createAsyncThunk(
  "patient/taskList",
  async () => {
    const res = await getTaskList();
    return res.data;
  }
);

/**
 * @更新任务
 */

export const fetchUpdateTaskAction = createAsyncThunk(
  "patient/updateTask",
  async (body: {
    examinationList: any[];
    examinationListUpdate: any[];
    taskList: Array<{
      id: number | null;
      timeOn: string | null;
      deadline: string | null;
      state: string;
    }>;
    userId: number;
    taskListUpdate: any[];
  }) => {
    const res = await updateTask(body);
    console.log(res);

    return res;
  }
);

/**
 * @病人详细信息
 */
export const fetchGetPatientDetailInfoAction = createAsyncThunk(
  "patient/patientDtailInfo",
  async (userId: number) => {
    const res = await getPatientDetailInfo(userId);
    console.log(res);
    return res.data;
  }
);

/**
 * @病人完成比例
 */

export const fetchPatientRateAction = createAsyncThunk(
  "patient/getRate",
  async (userId: number) => {
    const res = await getPatientRate(userId);
    console.log(res);

    return res.data;
  }
);
/**
 * @修改任务检查
 */
export const fetchUpdateTaskExaminationAction = createAsyncThunk(
  "patient/updateTaskExamination",
  async (body: {
    examinationList: {
      id: number;
      state: string | null;
      timeOn: string | null;
    }[];
    examinationListUpdate: any[];
    taskList: {
      deadline: string | null;
      id: number;
      state: string | null;
      timeOn: string | null;
    }[];
    taskListUpdate: any[];
    userId: number;
  }) => {
    const res = await updateTaskExamination(body);
    console.log(res);
    return res;
  }
);

/**
 * @修改病人信息
 */
export const fetchModifyPatientInforAction = createAsyncThunk(
  "patient/modify",
  async (params: {
    avatar?: string;
    createBy: string | null;
    createTime: string | null;
    deptId?: number;
    dischargeDate?: string;
    doctorId?: number;
    email?: string;
    familyHistory?: string;
    hospitalId?: number;
    hospitalizationDate?: string;
    id: number;
    illnessId?: number;
    image?: string;
    medicalRecord?: string;
    name?: string;
    nickName?: string;
    nurseId?: number;
    operation?: string;
    operationDate?: string;
    pastMedicalHistory?: string;
    programmeTitle: string | null;
    phonenumber?: string;
    programmeId?: number;
    sex?: string;
    remark: null;
    updateBy: null;
    state?: string;
    userId?: number;
    username?: string;
    updateTime?: string | null;
  }) => {
    const res = await modifyPatientInfor(params);
    console.log(res);
    return res;
  }
);

const patientSlice = createSlice({
  name: "patient",
  initialState,
  reducers: {
    changePatientsUserInfo(state, action) {
      state.patientUsersInfo = action.payload;
    },
  },
  extraReducers: (builder) => {
    builder
      .addCase(fetchPatientsUserInfoAction.fulfilled, (state, action) => {
        state.total = action.payload.total;
        state.patientUsersInfo = action.payload.rows;
      })
      .addCase(fetchDoctorListAction.fulfilled, (state, action) => {
        state.doctorList = action.payload;
      })
      .addCase(fetchNurseListAction.fulfilled, (state, action) => {
        state.nurseList = action.payload;
      })
      .addCase(fetchDeptListAction.fulfilled, (state, action) => {
        state.deptList = action.payload;
      })
      .addCase(fetchIllnessListAction.fulfilled, (state, action) => {
        state.illnessList = action.payload;
      })
      .addCase(fetchExaminationListAction.fulfilled, (state, action) => {
        state.examinationList = action.payload;
      })
      .addCase(fetchGetTaskExaminationAction.fulfilled, (state, action) => {
        state.modificationAndTaskList = action.payload;
      })
      .addCase(fetchGetTaskListAction.fulfilled, (state, action) => {
        state.taskList = action.payload;
      })
      .addCase(fetchGetPatientDetailInfoAction.fulfilled, (state, action) => {
        state.patientDetailInfo = action.payload;
      })
      .addCase(fetchPatientRateAction.fulfilled, (state, action) => {
        state.getRate = action.payload;
      });
  },
});
export const { changePatientsUserInfo } = patientSlice.actions;
export default patientSlice.reducer;
